﻿using StarsAdmin.Core.Extensions;
using System.Security.Cryptography;
using System.Text;

namespace StarsAdmin.Core.Helpers;

/// <summary>
/// 字符串帮助类
/// </summary>
public class StringHelper
{
    private static readonly string _chars = "0123456789";
    private static readonly char[] _constant = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };

    /// <summary>
    ///生成随机字符串，默认32位
    /// </summary>
    /// <param name="length"></param>
    /// <returns></returns>
    public static string GenerateRandom(int length = 32)
    {
        var randomNumber = new byte[length];
        using var rng = RandomNumberGenerator.Create();
        rng.GetBytes(randomNumber);
        return Convert.ToBase64String(randomNumber);
    }

    /// <summary>
    /// 根据系统指定的char[]生成随机字符串，默认32位
    /// </summary>
    /// <param name="length">随机数长度</param>
    /// <returns></returns>
    public static string GenerateRandomString(int length = 32)
    {
        StringBuilder newRandom = new StringBuilder();
        Random random = new Random();
        for (int i = 0; i < length; i++)
        {
            int index = random.Next(_constant.Length);
            char ch = _constant[index];
            newRandom.Append(ch);
        }
        return newRandom.ToString();
    }

    /// <summary>
    /// 生成随机数字，默认6位
    /// </summary>
    /// <param name="length"></param>
    /// <returns></returns>
    public static string GenerateRandomNumber(int length = 6)
    {
        var random = new Random();
        var a = Enumerable.Repeat(_chars, length).Select(s => s[random.Next(s.Length)]).ToArray();
        return new string(a);
    }

    /// <summary>
    /// 获取默认值
    /// </summary>
    /// <param name="value"></param>
    /// <param name="defaultValue"></param>
    /// <returns></returns>
    public static string GetCustomValue(string value, string defaultValue)
    {
        if (string.IsNullOrEmpty(value))
        {
            return defaultValue;
        } else
        {
            return value;
        }
    }

    /// <summary>
    /// 截取指定长度的字符串
    /// </summary>
    /// <param name="value"></param>
    /// <param name="length"></param>
    /// <returns></returns>
    public static string GetSubString(string value, int length, bool ellipsis = false)
    {
        if (string.IsNullOrEmpty(value))
        {
            return value;
        }
        if (value.Length > length)
        {
            value = value.Substring(0, length);
            if (ellipsis)
            {
                value += "...";
            }
        }
        return value;
    }

    /// <summary>
    /// 字符串转指定类型数组
    /// </summary>
    /// <param name="value"></param>
    /// <param name="split"></param>
    /// <returns></returns>
    public static T[] SplitToArray<T>(string value, char split)
    {
        T[] arr = value.Split(new string[] { split.ToString() }, StringSplitOptions.RemoveEmptyEntries).CastSuper<T>().ToArray();
        return arr;
    }
}